﻿using Microsoft.Win32;
using System;
using HighPerformanceLog.V2;

namespace ProcessBarDemo.UpdateFramework
{
    class AutoStart
    {
        public static void SetAutoStart(bool enable, string appName = "MyWpfApp")
        {
            try
            {
                string runKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
                string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;

                using (RegistryKey key = Registry.CurrentUser.OpenSubKey(runKey, true))
                {
                    if (enable)
                    {
                        key.SetValue(appName, $"\"{exePath}\"");
                    }
                    else
                    {
                        key.DeleteValue(appName, false);
                    }
                }
            }
            catch (Exception ex)
            {
                HWLogger.ErrorLog($"设置开机自启动失败，错误：{ex.Message}");
                throw new InvalidOperationException("设置开机启动失败", ex);
            }
        }
    }
}
